// miniprogram/pages/movementInfo/movementInfo.js
const db = wx.cloud.database();
const app = getApp();
const movement = db.collection('movements')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: null,
    movement: null,
    comments: [],
    myComment: '',
    to: '',
    time: null,
    anonymous: true
  },

  onChange: function (event) {
    if (event.detail.length < 1 && this.data.to != '' && event.detail[0] != '@') {
      this.setData({
        to: this.data.movement.userId
      })
      wx.showToast({
        title: '取消回复',
        icon: 'none'
      })
    }
    this.setData({
      myComment: event.detail
    })
  },

  onSend: function () {
    let time = new Date().getTime()
    var name = app.globalData.name
    var head = app.globalData.head
    var anonymous = false
    wx.showModal({
      title: '匿名评论？',
      content: '匿名评论时回复时回复仍可以传达给你',
      cancelColor: 'cancelColor',
      success: res => {
        wx.showToast({
          title: '回复中',
          icon: 'loading'
        })
        if (res.confirm) {
          name = app.globalData.randomName
          head = 'cloud://wx-vn1hy.7778-wx-vn1hy-1303008862/timg.jpg'
          anonymous = true
        }
        db.collection('comments').add({
          data: {
            id: this.data.id,
            comment: this.data.myComment,
            sendId: app.globalData.openid,
            toId: this.data.to,
            username: name,
            userhead: head,
            createTime: time,
            anonymous: anonymous
          }
        }).then(res => {
          this.setData({
            myComment: '',
            to: ''
          })
          wx.cloud.callFunction({
            name: 'updateComment',
            data: {
              id: this.data.id,
            },
            success: res=>{
              console.log(res)
            }
          })
          wx.hideToast()
          this.getComments()
        })
      }
    })

  },

  showComment: function (event) {
    wx.showModal({
      title: '评论',
      content: event.currentTarget.dataset.text,
      confirmText: '回复他',
      success: res => {
        if (res.confirm) {
          this.setData({
            to: event.currentTarget.dataset.id,
            myComment: '@' + event.currentTarget.dataset.name + ' '
          })
        } else if (res.cancel) {
          this.setData({
            to: '',
            myComment: ''
          })
          wx.showToast({
            title: '取消回复',
            icon: 'none'
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var time = new Date().getTime()
    //console.log(options.id)
    this.setData({
      id: options.id,
      time: time,
    })
    try {
      movement.where({
        _id: options.id
      }).get().then(res => {
        if (res.data.length == 0) {
          wx.showToast({
            title: '该树洞已被删除，你可以继续查看评论',
            icon: 'none'
          })
        } else {
          wx.cloud.callFunction({
            name: 'updateWatch',
            data: {
              id: res.data[0]._id,
            },
            complete: res => {
              //this.onLoad()
            }
          })
          this.setData({
            movement: res.data[0],
            to: res.data[0]._openid,
            anonymous: res.data[0].anonymous
          })
        }
      })
    } catch (error) {
      //console.log(error)
    }
    this.getComments()
  },

  getComments: function () {
    db.collection("comments").where({
      id: this.data.id
    }).orderBy('createTime', 'desc').get().then(res => {
      this.setData({
        comments: res.data
      })
    })
  },

  onLocation: function (event) {
    console.log(event)
    wx.openLocation(event.currentTarget.dataset.loc)
  },

  onPicture: function (event) {
    console.log(event)
    wx.previewImage({
      urls: event.currentTarget.dataset.pic // 需要预览的图片http链接列表
    })
  },

  onChat: function (event) {
    if (this.data.anonymous) {
      wx.navigateTo({
        url: '../anonymousChat/index?id=' + event.currentTarget.dataset.id + '&name=' + event.currentTarget.dataset.name,
      })
    } else {
      wx.navigateTo({
        url: '../privateChat/index?id=' + event.currentTarget.dataset.id + '&name=' + event.currentTarget.dataset.name,
      })
    }
  }
})